import { Menu } from "@/types"
import enquireJs from "enquire.js"

/**
 * @description 使用递归扁平化菜单，方便添加动态路由
 * @param {Array} menuList 菜单列表
 * @returns {Array}
 */
export function getFlatMenuList(menuList: Menu.MenuOptions[]): Menu.MenuOptions[] {
  let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList))
  return newMenuList.flatMap(item => [item, ...(item.children ? getFlatMenuList(item.children) : [])])
}

/**
 * @description 使用递归过滤出需要渲染在左侧菜单的列表 (需剔除 isHide == true 的菜单)
 * @param {Array} menuList 菜单列表
 * @returns {Array}
 * */
export function getShowMenuList(menuList: Menu.MenuOptions[]) {
  let newMenuList: Menu.MenuOptions[] = JSON.parse(JSON.stringify(menuList))
  return newMenuList.filter(item => {
    item.children?.length && (item.children = getShowMenuList(item.children))
    return !item.meta?.isHide
  })
}
/**
 * @description 使用递归过滤出当前用户角色身份具有权限访问的菜单 (需剔除 permission里不包含当前用户角色的菜单，无permission为公共路由)
 * @param {Array} menu 菜单列表
 * @returns {Array}
 * */
export const filterMenuByRole = (menu: any, roles: string[]) => {
  return menu
    .filter((item: any) => {
      if (item.meta?.permission) {
        // 如果菜单项有指定角色
        for (const role of roles) {
          if (item.meta.permission.includes(role)) {
            return true
          }
        }
        return false
      } else {
        // 如果菜单项没有指定角色，默认为公共菜单项，对所有角色可见
        return true
      }
    })
    .map((item: any) => {
      if (item.children) {
        // 递归过滤子菜单项
        item.children = filterMenuByRole(item.children, roles)
      }
      return item
    })
}
/**
 * 获取当前使时间
 * @returns  返回格式如2023-6-18 13:00:00
 */
export const getCurrentTime = (): string => {
  const now: Date = new Date()
  const year: number = now.getFullYear()
  const month: string = String(now.getMonth() + 1).padStart(2, "0")
  const day: string = String(now.getDate()).padStart(2, "0")
  const hours: string = String(now.getHours()).padStart(2, "0")
  const minutes: string = String(now.getMinutes()).padStart(2, "0")
  const seconds: string = String(now.getSeconds()).padStart(2, "0")

  const formattedTime: string = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  return formattedTime
}

/**
 * 深拷贝
 */
export const deepCopy = <T>(value: T): T => {
  if (typeof value === "object" && value !== null) {
    return JSON.parse(JSON.stringify(value))
  } else {
    return value
  }
}

export const replaceCodeBlocks = (responseContent: string) => {
  const pattern = /\[code\]([\s\S]*?)(\[\/code\]|$)/g
  const replacedContent = responseContent.replace(pattern, '<pre><code class="javascript">$1</code></pre>')
  return replacedContent
}

/**
 * 将 Unix 时间戳转换为指定格式的日期字符串
 * @param {number} unixTimestamp - Unix 时间戳，单位为秒
 * @returns {string} - 格式化后的日期字符串，格式为 "YYYY-MM-DD HH:mm:ss"
 */
export const formatUnixTimestamp = (unixTimestamp: number): string => {
  const timestamp = unixTimestamp * 1000 // 将秒转换为毫秒
  const date = new Date(timestamp)

  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, "0")
  const day = String(date.getDate()).padStart(2, "0")

  const hours = String(date.getHours()).padStart(2, "0")
  const minutes = String(date.getMinutes()).padStart(2, "0")
  const seconds = String(date.getSeconds()).padStart(2, "0")

  const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`

  return formattedDate
}

/**
 * 将对象2中的属性值赋给对象1
 * @param {Object} obj1 - 对象1
 * @param {Object} obj2 - 对象2
 * @returns {Object} - 更新后的对象1
 */
export const assignProperties = <T = any>(obj1: T, obj2: T) => {
  for (let key in obj1) {
    if (obj2?.hasOwnProperty(key)) {
      obj1[key] = obj2[key]
    }
  }
}

export const enquireScreen = (size: string, cb: Function) => {
  enquireJs.register(`screen and (max-width:${size})`, {
    match: () => {
      cb && cb(true)
    },
    unmatch: () => {
      cb && cb(false)
    }
  })
}
